
*-----------------------------------------------------------------------------*
*------- 						Do file 1						  ------------*
*-----------------------------------------------------------------------------*

*This do file selects the variables from EES 2014 and 2019, codes the variables and merges the data.



cd "${data}"

use "EES2019.dta", clear

rename D11 familypoorrich // Taking everything into account, at about what level is your family's standard of living? If you think of a scale from 1 to 7, where 1 means a poor family, 7 a rich family, and the other numbers are for the positions in between, about where would you place your family?

*rename D2_1 education
rename EDU education
rename D4_1 birthyear
rename hCountry country
rename countrycode country_
rename Q11 selfplacement
rename q13_* partyplacement_*
rename Q14_1 regulation // control of economy
rename Q14_2 redistribution
rename Q14_3 samesexmarriage
rename Q14_4 laworder
rename Q14_5 immigration
rename Q14_6 environment
rename Q23 euintegration
rename q24_* euintegration_party*

gen spending = . 
gen spending_dk = . 

gen selfplacement_dk = selfplacement 
gen regulation_dk = regulation
gen redistribution_dk = redistribution
gen samesexmarriage_dk = samesexmarriage
gen laworder_dk = laworder
gen immigration_dk = immigration
gen environment_dk = environment
gen euintegration_dk = euintegration
recode selfplacement_dk (97=.) (98=99)
recode regulation_dk (99=.) (98=99)
recode redistribution_dk (99=.) (98=99)
recode samesexmarriage_dk (99=.) (98=99)
recode laworder_dk (99=.) (98=99)
recode immigration_dk (99=.) (98=99)
recode environment_dk (99=.) (98=99)
recode euintegration_dk (97=.) (98=99)

gen age = 2019 - birthyear 

gen voted = 1
replace voted = 0 if Q9 == 97
replace voted = . if Q9 == 99 | Q9 == 98 | Q9 == 96

recode education (99=.)
*recode education (97=.) (99=.)
recode selfplacement (11/99=.)
recode regulation (11/99=.)
recode redistribution (11/99=.)
recode samesexmarriage (11/99=.)
recode laworder (11/99=.)
recode immigration (11/99=.) (0=10) (1=9) (2=8) (3=7) (4=6) (6=4) (7=3) (8=2) (9=1) (10=0)
recode environment (11/99=.)
recode euintegration (11/99=.) (0=10) (1=9) (2=8) (3=7) (4=6) (6=4) (7=3) (8=2) (9=1) (10=0)
recode familypoorrich (97/99=.) 

gen gender = . 
replace gender = 1 if D3==2 
replace gender = 0 if D3==1

/*
bysort country : egen mean_familypoorrich = mean(familypoorrich)	

gen welloff = .
replace welloff = 1 if familypoorrich > mean_familypoorrich
replace welloff = 0 if familypoorrich < mean_familypoorrich
*/

bysort country : egen mean_educationage = mean(education)	

gen educated = .
replace educated = 1 if education > mean_educationage
replace educated = 0 if education < mean_educationage


bysort country : egen mean_familypoorrich = mean(familypoorrich)	

gen welloff = .
replace welloff = 1 if familypoorrich > mean_familypoorrich
replace welloff = 0 if familypoorrich < mean_familypoorrich


keep country country_ selfplacement regulation redistribution ///
samesexmarriage laworder immigration environment euintegration voted age education welloff gender selfplacement_dk regulation_dk redistribution_dk samesexmarriage_dk laworder_dk immigration_dk environment_dk euintegration_dk spending spending_dk




drop country_

gen country_ = ""
replace country_ = "Austria" if country==1
replace country_ = "Belgium" if country==2
replace country_ = "Bulgaria" if country==3
replace country_ = "Croatia" if country==4
replace country_ = "Cyprus" if country==5
replace country_ = "Czech Republic" if country==6
replace country_ = "Denmark" if country==7
replace country_ = "Germany" if country==8
replace country_ = "Estonia" if country==9
replace country_ = "Finland" if country==10
replace country_ = "France" if country==11
replace country_ = "Greece" if country==12
replace country_ = "Hungary" if country==13
replace country_ = "Ireland" if country==14
replace country_ = "Italy" if country==15
replace country_ = "Latvia" if country==16
replace country_ = "Lithuania" if country==17
replace country_ = "Luxembourg" if country==18
replace country_ = "Malta" if country==19
replace country_ = "Netherlands" if country==20
replace country_ = "Poland" if country==21
replace country_ = "Portugal" if country==22
replace country_ = "Romania" if country==23
replace country_ = "Slovenia" if country==24
replace country_ = "Slovakia" if country==25
replace country_ = "Spain" if country==26
replace country_ = "Sweden" if country==27
replace country_ = "United Kingdom" if country==28

gen year = 2019 
save "2019.dta" ,replace


/* 
country selfplacement partyplacement_1 partyplacement_2 partyplacement_3 partyplacement_4 partyplacement_5 partyplacement_6 partyplacement_7 partyplacement_8 partyplacement_9 regulation redistribution samesexmarriage laworder immigration environment euintegration euintegration_party1 euintegration_party2 euintegration_party3 euintegration_party4 euintegration_party5 euintegration_party6 euintegration_party7 euintegration_party8 euintegration_party9 voted country_
*/

*------------------------------------------------------------------------------*

use "EES2014.dta", clear


rename vd11 age
rename d61 socialladder
rename d8 education 
rename b country
rename qpp13 selfplacement
rename qpp14_* partyplacement_*
rename qpp17_1 regulation
rename qpp17_2 redistribution
rename qpp17_3 spending
rename qpp17_4 samesexmarriage
rename qpp17_5 laworder
rename qpp17_6 immigration
rename qpp17_8 environment
rename qpp18 euintegration
rename qpp19_* euintegration_party*
rename qpp4 voted

gen selfplacement_dk = selfplacement 
gen regulation_dk = regulation
gen redistribution_dk = redistribution
gen samesexmarriage_dk = samesexmarriage
gen laworder_dk = laworder
gen immigration_dk = immigration
gen environment_dk = environment
gen euintegration_dk = euintegration
gen spending_dk = spending

recode selfplacement_dk (-8=.) (-9=99)
recode regulation_dk (-8=.) (-9=99)
recode redistribution_dk (-8=.) (-9=99)
recode samesexmarriage_dk (-8=.) (-9=99)
recode laworder_dk (-8=.) (-9=99)
recode immigration_dk (-8=.) (-9=99)
recode environment_dk (-8=.) (-9=99)
recode euintegration_dk (-8=.) (-9=99)
recode spending_dk (-8=.) (-9=99)


recode education (-9=.) (-8=.) (5=1) (4=97) // 4- still studying 5- no full time education
recode selfplacement (-9/-1=.)
recode selfplacement (1=0) (2=1) (3=2) (4=3) (5=4) (6=5) (7=6) (8=7) (9=8) (10=9) (11=10)
recode regulation (-9/-1=.) (1=0) (2=1) (3=2) (4=3) (5=4) (6=5) (7=6) (8=7) (9=8) (10=9) (11=10)
recode redistribution (-9/-1=.) (1=0) (2=1) (3=2) (4=3) (5=4) (6=5) (7=6) (8=7) (9=8) (10=9) (11=10)
recode spending (-9/-1=.) (1=0) (2=1) (3=2) (4=3) (5=4) (6=5) (7=6) (8=7) (9=8) (10=9) (11=10)
recode samesexmarriage (-9/-1=.) (1=0) (2=1) (3=2) (4=3) (5=4) (6=5) (7=6) (8=7) (9=8) (10=9) (11=10)
recode laworder (-9/-1=.) (1=0) (2=1) (3=2) (4=3) (5=4) (6=5) (7=6) (8=7) (9=8) (10=9) (11=10)
recode immigration (-9/-1=.) (1=10) (2=9) (3=8) (4=7) (5=6) (6=5) (7=4) (8=3) (9=2) (10=1) (11=0)
recode environment (-9/-1=.) (1=0) (2=1) (3=2) (4=3) (5=4) (6=5) (7=6) (8=7) (9=8) (10=9) (11=10)
recode euintegration (-9/-1=.) (1=10) (2=9) (3=8) (4=7) (5=6) (6=5) (7=4) (8=3) (9=2) (10=1) (11=0)



recode selfplacement_dk (1=0) (2=1) (3=2) (4=3) (5=4) (6=5) (7=6) (8=7) (9=8) (10=9) (11=10)
recode regulation_dk (1=0) (2=1) (3=2) (4=3) (5=4) (6=5) (7=6) (8=7) (9=8) (10=9) (11=10)
recode redistribution_dk (1=0) (2=1) (3=2) (4=3) (5=4) (6=5) (7=6) (8=7) (9=8) (10=9) (11=10)
recode spending_dk (1=0) (2=1) (3=2) (4=3) (5=4) (6=5) (7=6) (8=7) (9=8) (10=9) (11=10)
recode samesexmarriage_dk (1=0) (2=1) (3=2) (4=3) (5=4) (6=5) (7=6) (8=7) (9=8) (10=9) (11=10)
recode laworder_dk (1=0) (2=1) (3=2) (4=3) (5=4) (6=5) (7=6) (8=7) (9=8) (10=9) (11=10)
recode immigration_dk (1=10) (2=9) (3=8) (4=7) (5=6) (6=5) (7=4) (8=3) (9=2) (10=1) (11=0)
recode environment_dk (1=0) (2=1) (3=2) (4=3) (5=4) (6=5) (7=6) (8=7) (9=8) (10=9) (11=10)
recode euintegration_dk (1=10) (2=9) (3=8) (4=7) (5=6) (6=5) (7=4) (8=3) (9=2) (10=1) (11=0)


keep country selfplacement spending regulation redistribution ///
samesexmarriage laworder immigration environment euintegration voted d10 education age socialladder selfplacement_dk regulation_dk redistribution_dk samesexmarriage_dk laworder_dk immigration_dk environment_dk euintegration_dk spending spending_dk


recode socialladder (-8=.)

replace voted = 0 if voted == 2

gen gender = . 
replace gender = 1 if d10==2
replace gender = 0 if d10==1
drop d10 

bysort country : egen mean_socialladder = mean(socialladder)	

gen welloff = .
replace welloff = 1 if socialladder > mean_socialladder
replace welloff = 0 if socialladder < mean_socialladder


gen country_ = ""
replace country_ = "Belgium" if country==1
replace country_ = "Denmark" if country==2
replace country_ = "Greece" if country==4
replace country_ = "Spain" if country==5
replace country_ = "Finland" if country==6
replace country_ = "France" if country==7
replace country_ = "Ireland" if country==8
replace country_ = "Italy" if country==9
replace country_ = "Luxembourg" if country==10
replace country_ = "Netherlands" if country==11
replace country_ = "Austria" if country==12
replace country_ = "Portugal" if country==13
replace country_ = "Sweden" if country==14
replace country_ = "Germany" if country==20 | country==21
replace country_ = "United Kingdom" if country==22
replace country_ = "Northern Ireland" if country==23
replace country_ = "Bulgaria" if country==31
replace country_ = "Cyprus" if country==32
replace country_ = "Czech Republic" if country==33
replace country_ = "Estonia" if country==34
replace country_ = "Croatia" if country==46
replace country_ = "Hungary" if country==35
replace country_ = "Latvia" if country==36
replace country_ = "Lithuania" if country==37
replace country_ = "Malta" if country==38
replace country_ = "Poland" if country==39
replace country_ = "Romania" if country==40
replace country_ = "Slovenia" if country==42
replace country_ = "Slovakia" if country==41

gen year = 2014 
save "2014.dta", replace

use "2019.dta", clear
append using 2014.dta

drop country 
encode country_, generate(country)

gen country_year = country_+"_"+string(year)
encode country_year, generate(country_year_)

save "dataset.dta", replace
 
 
 use "yearselectionsandees.dta", clear  // this is data for the year of the previous election
 
 gen country_year = country_+"_"+string(year)
 gen difference = year - electionyear
 
keep country_year difference
 
save "timedifference.dta", replace 
 


 use "dataset.dta", clear 
  merge m:1 country_year using "timedifference.dta"
drop _merge
 save "dataset.dta", replace
 
bysort country_year: egen turnout_r = mean(voted)
 
gen samplelimit = 1 if age!=. & education!=. & welloff!=. & gender!=.
 

 rename age age_cont
 gen age = . 
 replace age = 0 if age_cont>15 & age_cont<25 
 replace age = 1 if age_cont>24 & age_cont<40
 replace age = 2 if age_cont>39 & age_cont<54
 replace age = 3 if age_cont>54 & age_cont<70
 replace age = 4 if age_cont>69 & age_cont<100
 
  save "dataset.dta", replace
 
 
 
 
